//index.
//获取应用实例
var utils = require("../../utils/util.js");
var valid = require("../../utils/valid.js");
var net = require("../../utils/net.js");
const app = getApp()
var posting =false;
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    tipmsg:"",
    tipno:0,
    mobile:"",
    psw: "",
    posting: false, 
    shop:[],
    shopids:[],
    shopindex:0
  },
  changeShop(e){
    this.setData({
      shopindex:e.detail.value
    })
  },
  input: function (e) {
    var value = e.detail.value + "";
    var key = e.currentTarget.id + "";
    this.setData({
      [key]: value,
    });
    if (key == "mobile") {
      if (value.length == 11)
        this.getshoplist();
      else
        this.setData({
          shopids: [],
          shop: []
        })
    }
  },
  Confirm:function(){
    this.setData({
      hidden:true
    })
  }, getshoplist() {
    var that = this;
    wx.showLoading({});
    net.request({
      url: '/GetShopList',
      data: {
        mobile: that.data.mobile
      }, success(res) {
        var dt = res.data;
        if (dt.flag) {
          var ids=[], shops = [];
          for (let i = 0, len = dt.data.length; i < len; ++i) {
            ids.push(dt.data[i]["id"])
            shops.push(dt.data[i]["shop_name"])
          }
          that.setData({
            shop: shops,
            shopids: ids
          })
        } else {
          that.showtip(2, dt.msg);
        }
      }, fail() {
        that.showtip(2,"系统错误，请联系技术人员");
      }, complete() {
        wx.hideLoading();
      }
    })
  },
  login(){
    var that = this;
    if (posting) return;
    posting  =true;
    wx.showLoading({})
    valid.login(this.data.mobile, this.data.psw, this.data.shopids[this.data.shopindex],{
      success(code,msg){
        that.showtip(3, msg);
        wx.switchTab({
          url: '../newhome/newhome',
        })
        posting = false;
        wx.hideLoading()
      }, fail(code, msg) {
        posting = false;
        that.showtip(code, msg);
        wx.hideLoading()
      },complete(){
          posting = false;
      }
    });
  },
  showtip(lv,msg){
    this.setData({
      tipno: lv,
      tipmsg:msg
    })
  },
  goreset(){
    utils.Forward("../resetpsw/resetpsw");
  },
  goforget(){
    utils.Forward("../forget/forget");
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      console.log(1);
      // this.setData({
      //   userInfo: app.globalData.userInfo,
      // })
    } else if (this.data.canIUse){
      console.log(2);
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
        })
      }
    } else {
      console.log(3);
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo
    })
  }
})
